//
//  MyGolfReserveController.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/6/28.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit
import MJRefresh

class MyGolfReserveController: CBBaseViewController {
    
    var tbReserve:UITableView!
    var arrData:[MyGolfReserveModel] = [MyGolfReserveModel]()
    let CELL_ID = "MY_GOLF_RESERVE_CELL_ID"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "我的预约"
        self.view.backgroundColor = UIColor(hex:0xf2f2f2)
        
        tbReserve = UITableView(frame: .zero, style: .plain)
        tbReserve.rowHeight = 95
        tbReserve.separatorStyle = .none
        tbReserve.dataSource = self
        tbReserve.tableFooterView = UIView()
        tbReserve.register(UINib(nibName: "MyGolfReserveCell", bundle: nil), forCellReuseIdentifier: CELL_ID)
        tbReserve.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
//            self?.curPage = 1
            self?.initData()
        })
//        tbReserve.mj_footer = MJRefreshFooter(refreshingBlock: {  [unowned self] in
//            self.curPage += 1
//            self.initData()
//        })
//        tbReserve.mj_footer.isHidden = true
        self.view.addSubview(tbReserve)
        
        tbReserve.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        tbReserve.mj_header.beginRefreshing()
        
    }
    
//    var curPage = 1
//    let pageSize = 20
    func initData() {
        NetworkTool.requestData(url: ActionURL.GET_MY_GOLF_RESERVE, param: [
            "uid":MainTools.shared.userModel?.uid ?? "",
//            "page":curPage,
//            "perPage":pageSize
            ])
            .map { (json) -> [MyGolfReserveModel]? in
                return MyGolfReserveModel.mj_objectArray(withKeyValuesArray: json["rows"].dictionaryValue["data"]?.arrayObject) as? [MyGolfReserveModel]
            }.catchErrorJustReturn(nil)
            .subscribe(onNext: { [unowned self] tmpArr in
                self.tbReserve.mj_header.endRefreshing()
//                self.tbReserve.mj_footer.endRefreshing()
                if let arrModel = tmpArr {
//                    if self.curPage == 1 {
//                        self.arrData = arrModel
//                    } else {
//                        self.arrData.append(contentsOf: arrModel)
//                    }
//                    self.tbReserve.mj_footer.isHidden = (arrModel.count < self.pageSize)
                    
                    self.arrData = arrModel
                    self.tbReserve.reloadData()
                }
            }).disposed(by: disposeBag)
    }

}

extension MyGolfReserveController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as? MyGolfReserveCell ?? MyGolfReserveCell()
        cell.model = arrData[indexPath.row]
        return cell
    }
    
}
